package com.swgk.core.view.fragment;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.swgk.core.R;
import com.swgk.core.base.AppFragment;
import com.swgk.core.databinding.FragEnterpriseRecMenuBinding;
import com.swgk.core.view.adapter.EnterpriseRecMenuAdapter;
import com.swgk.core.view.entity.EnterpriseRecEntity;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseRecMenuFragment extends AppFragment {

    public static final String KEY_DATA = "data";
    public static final String COLUMN_COUNT = "column_count";
    public static final String ROW_COUNT = "row_count";

    FragEnterpriseRecMenuBinding binding;

    public static EnterpriseRecMenuFragment newInstance(List<EnterpriseRecEntity> type, int column, int row) {
        EnterpriseRecMenuFragment fragment = new EnterpriseRecMenuFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(KEY_DATA, (ArrayList<? extends Parcelable>) type);
        bundle.putInt(COLUMN_COUNT, column);
        bundle.putInt(ROW_COUNT, row);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.frag_enterprise_rec_menu, container, false);
        init();
        return binding.getRoot();
    }

    private void init() {
        List<EnterpriseRecEntity> data = getArguments().getParcelableArrayList(KEY_DATA);
        int column = (int) getArguments().get(COLUMN_COUNT);
        int row = (int) getArguments().get(ROW_COUNT);

        EnterpriseRecMenuAdapter adapter = new EnterpriseRecMenuAdapter(getActivity(), null);
        adapter.setCount(column * row);
        adapter.setDatas(data);
        binding.rlvMenu.setLayoutManager(new GridLayoutManager(getActivity(), column));
        binding.rlvMenu.setAdapter(adapter);
    }
}
